<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="公司代码" prop="company_code">
        <el-input
          v-model="queryParams.company_code"
          placeholder="请输入公司代码"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="中文名称" prop="name_c">
        <el-input
          v-model="queryParams.name_c"
          placeholder="请输入中文名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-action="'company@create'"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column prop="company_code" label="公司代码" align="center" />
      <el-table-column prop="brief_code" label="简码" align="center" />
      <el-table-column prop="name_c" label="中文名称" align="center" />
      <el-table-column prop="name_e" label="英文名称" align="center" />
      <el-table-column prop="addr_c" label="中文地址" align="center" />
      <el-table-column prop="addr_e" label="英文地址" align="center" />
      <el-table-column prop="phone" label="电话" align="center" />
      <el-table-column prop="fax" label="传真" align="center" />
      <el-table-column prop="tax" label="税号" align="center" />
      <el-table-column prop="email" label="邮箱" align="center" />
      <el-table-column prop="post_code" label="邮政编码" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-action="'company@update'"
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
          >修改
          </el-button>
          <el-button
            v-action="'company@delete'"
            size="mini"
            type="danger"
            @click="handleDel(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.limit" @pagination="getList" />
    <!-- 新增/修改 -->
    <AddEdit :open.sync="open" :title="title" :form="form" @getList="getList" />
  </div>
</template>
<script>
import { companyList, companyDel } from '@/api/basics'
import AddEdit from './AddEdit'
export default {
  name: 'Index',
  components: {
    AddEdit
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      open: false,
      title: '',
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      form: {},
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        company_code: undefined,
        name_c: undefined
      },
      show: false,
      ids: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      companyList(this.queryParams).then(response => {
        if (response.code === 10000) {
          that.list = response.data
          that.total = response.count
          that.loading = false
        }
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {}
      this.open = true
      this.title = '添加'
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      this.form = row
      this.open = true
      this.title = '编辑'
    },
    /** 删除按钮操作 */
    handleDel(row) {
      console.log(row)
      const that = this
      that.$confirm('是否要删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        companyDel({ id: row.id.toString() }).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        company_code: undefined,
        name_c: undefined
      }
      this.handleQuery()
    }
  }
}

</script>

